package servlet;

import dao.StudentDao;
import pojo.Student;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * @author konley
 * @date 2020-06-10 20:22
 */
@WebServlet("/StudentServlet")
public class StudentServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String m = request.getParameter("m");
        String msg = "更新成功！";
        if(m.equals("update")){
            Student stu = new Student();
            int sid = Integer.parseInt(request.getParameter("sid"));
            stu.setSid(sid);
            stu.setName(request.getParameter("name"));
            stu.setPassword(request.getParameter("password"));
            stu.setSex(request.getParameter("sex"));
            stu.setDept(request.getParameter("dept"));
            stu.setTel(request.getParameter("tel"));
            boolean rs = new StudentDao().studentUpdate(stu);
            if(!rs) {
                msg = "更新失败！";
            }
            //重新查询stu结果
            stu = new StudentDao().getById(sid);
            request.setAttribute("msg",msg);
            request.getSession(true).setAttribute("student",stu);
            request.getRequestDispatcher("/studentView.jsp").forward(request,response);
        }

    }
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String m = request.getParameter("m");
        if(m.equals("doUpdate")){
            String id = request.getParameter("id");
            Student student = new StudentDao().getById(Integer.parseInt(id));
            request.setAttribute("student",student);
            request.getRequestDispatcher("/studentUpdate.jsp").forward(request,response);
        }
    }
}
